package com.myeclipse.search;

import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.search.ui.ISearchPage;
import org.eclipse.search.ui.ISearchPageContainer;
//import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.NewSearchUI;
//import org.eclipse.swt.widgets.Button;
//import org.eclipse.swt.widgets.Composite;
//import org.eclipse.swt.widgets.Group;
//import org.eclipse.swt.widgets.Label;
//import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SearchPage extends DialogPage implements ISearchPage {

	private ISearchPageContainer container;
	private String searchString="";
	private String sourceURL="";
	private String language="";
	private String fileExtention="";
	private Text fileExtText;
	private Text SearchStringText;
	public SearchPage() {
		// TODO Auto-generated constructor stub
	}

	public SearchPage(String title) {
		super(title);
		// TODO Auto-generated constructor stub
	}

	public SearchPage(String title, ImageDescriptor image) {
		super(title, image);
		// TODO Auto-generated constructor stub
	}
	
	/*Set search dialog layout in this function. */
	@Override
	public void createControl(Composite parent) {
		// TODO Auto-generated method stub
		Composite root = new Composite(parent, SWT.NONE);
		root.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		root.setLayout(layout);
		Label label1=new Label(root,SWT.LEFT);
		label1.setText("Search String:");
		GridData gridData=new GridData();
		gridData.horizontalSpan=2;
		label1.setLayoutData(gridData);
		
		SearchStringText=new Text(root,SWT.BORDER);
		gridData=new GridData();
		//gridData.horizontalSpan=2;
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		SearchStringText.setLayoutData(gridData);
		
		Button regularExp=new Button(root,SWT.CHECK);
		regularExp.setText("Regular Expression");
		gridData=new GridData();
		//gridData.horizontalSpan=2;
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		regularExp.setLayoutData(gridData);
		
		//Language Filter------------------------------------------------------
		Group langGroup=new Group(root,SWT.NULL);
		langGroup.setText("Language Filter");
		GridLayout langLayout=new GridLayout();
		langLayout.numColumns=4;
		langGroup.setLayout(langLayout);
		gridData=new GridData();
		gridData.horizontalSpan=2;
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langGroup.setLayoutData(gridData);
		final Button langButtonAll=new Button(langGroup,SWT.RADIO);
		langButtonAll.setText("ALL");
		langButtonAll.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language="";
			//langButtonAll.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonAll.setLayoutData(gridData);
		final Button langButtonJava=new Button(langGroup,SWT.RADIO);
		langButtonJava.setText("Java");
		langButtonJava.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:java";
			//langButtonJava.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonJava.setLayoutData(gridData);
		final Button langButtonJavascript=new Button(langGroup,SWT.RADIO);
		langButtonJavascript.setText("Javascript");
		langButtonJavascript.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:javascript";
			//langButtonJavascript.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonJavascript.setLayoutData(gridData);
		final Button langButtonCsharp=new Button(langGroup,SWT.RADIO);
		langButtonCsharp.setText("C#");
		langButtonCsharp.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:c#";
			//langButtonCsharp.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonCsharp.setLayoutData(gridData);
		final Button langButtonCpp=new Button(langGroup,SWT.RADIO);
		langButtonCpp.setText("C++");
		langButtonCpp.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:c++";
			//langButtonCpp.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonCpp.setLayoutData(gridData);
		final Button langButtonC=new Button(langGroup,SWT.RADIO);
		langButtonC.setText("C");
		langButtonC.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:c";
			//langButtonC.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonC.setLayoutData(gridData);
		final Button langButtonSQL=new Button(langGroup,SWT.RADIO);
		langButtonSQL.setText("SQL");
		langButtonSQL.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:sql";
			//langButtonSQL.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonSQL.setLayoutData(gridData);
		final Button langButtonPython=new Button(langGroup,SWT.RADIO);
		langButtonPython.setText("Python");
		langButtonPython.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:python";
			//langButtonPython.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonPython.setLayoutData(gridData);
		final Button langButtonAsp=new Button(langGroup,SWT.RADIO);
		langButtonAsp.setText("ASP.Net");
		langButtonAsp.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			language=" lang:asp.net";
			//langButtonAsp.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		langButtonAsp.setLayoutData(gridData);
		langButtonAll.setSelection(true);
		
		//Source URL Filter------------------------------------------------------
		Group urlGroup=new Group(root,SWT.NULL);
		urlGroup.setText("Source URL Filter");
		GridLayout urlLayout=new GridLayout();
		urlLayout.numColumns=4;
		urlGroup.setLayout(urlLayout);
		gridData=new GridData();
		gridData.horizontalSpan=2;
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlGroup.setLayoutData(gridData);
		final Button urlButtonAll=new Button(urlGroup,SWT.RADIO);
		urlButtonAll.setText("ALL");
		urlButtonAll.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL="";
			//urlButtonAll.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonAll.setLayoutData(gridData);
		final Button urlButtonGithub=new Button(urlGroup,SWT.RADIO);
		urlButtonGithub.setText("GitHub");
		urlButtonGithub.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL=" url:github";
			//urlButtonGithub.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonGithub.setLayoutData(gridData);
		final Button urlButtonGoogle=new Button(urlGroup,SWT.RADIO);
		urlButtonGoogle.setText("Google Code");
		urlButtonGoogle.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL=" url:google";
			//urlButtonGoogle.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonGoogle.setLayoutData(gridData);
		final Button urlButtonCodeplex=new Button(urlGroup,SWT.RADIO);
		urlButtonCodeplex.setText("CodePlex");
		urlButtonCodeplex.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL=" url:codeplex";
			//urlButtonCodeplex.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonCodeplex.setLayoutData(gridData);
		final Button urlButtonSourceforge=new Button(urlGroup,SWT.RADIO);
		urlButtonSourceforge.setText("SourceForge");
		urlButtonSourceforge.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL=" url:sourceforge";
			//urlButtonSourceforge.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonSourceforge.setLayoutData(gridData);
		final Button urlButtonBitbucket=new Button(urlGroup,SWT.RADIO);
		urlButtonBitbucket.setText("Bitbucket");
		urlButtonBitbucket.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL=" url:bitbucket";
			//urlButtonBitbucket.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonBitbucket.setLayoutData(gridData);
		final Button urlButtonFedora=new Button(urlGroup,SWT.RADIO);
		urlButtonFedora.setText("Fedora Source Code");
		urlButtonFedora.addSelectionListener(new SelectionAdapter(){
		public void widgetSelected(SelectionEvent event){
			sourceURL=" url:fedora";
			//urlButtonFedora.setText("I was clicked");
		}
		});
		gridData=new GridData();
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		urlButtonFedora.setLayoutData(gridData);
		urlButtonAll.setSelection(true);
		
		//File Extention Filter------------------------------------------------------
		Group fileExtGroup=new Group(root,SWT.NULL);
		fileExtGroup.setText("File Extention Filter");
		fileExtText = new Text(fileExtGroup, SWT.BORDER);
		gridData=new GridData();
		//gridData.horizontalSpan=2;
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		fileExtText.setLayoutData(gridData);
		Label label = new Label(fileExtGroup, SWT.NONE);
		gridData=new GridData();
		//gridData.horizontalSpan=2;
		gridData.horizontalAlignment=GridData.FILL;
		gridData.grabExcessHorizontalSpace=true;
		label.setLayoutData(gridData);
		label.setText("Example:cpp,java,html...");
		
		
		GridLayout fileExtLayout=new GridLayout();
		fileExtGroup.setLayout(fileExtLayout);
		setControl(root);
	}

	/* After the user click the search button on the search page dialog, will run this function.
	 * entrance to the SearchQuery */
	@Override
	public boolean performAction() {
		// TODO Auto-generated method stub
		
		NewSearchUI.activateSearchResultView();
		
		if(fileExtText.getText()=="")
			fileExtention="";
		else {
			fileExtention=" ext:"+fileExtText.getText();
		}
		searchString=SearchStringText.getText()+language+sourceURL+fileExtention;
		System.out.println(searchString);
		
		NewSearchUI.runQueryInBackground(new SearchQuery(searchString,container));
		return true;
	}


	protected ISearchPageContainer getContainer()
	{
		return this.container;
	}
	
	@Override
	public void setContainer(ISearchPageContainer container) {
		// TODO Auto-generated method stub
		this.container=container;
	}

}
